package com.work.game.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.work.game.R;
import com.work.game.home.ui.activity.GoodDetailActivity;
import com.work.game.home.ui.model.Good;
import com.zzhoujay.richtext.RichText;

import java.util.List;

public class ItemCateHomeAdapter extends RecyclerView.Adapter<CateHomeViewHolder>{
    Context context;
    public List<Good> goodList;

    public ItemCateHomeAdapter(Context context, List<Good> goodList) {
        this.context = context;
        this.goodList = goodList;
    }
    public void refreshData(List<Good> newData){
        goodList.clear();
        goodList.addAll(newData);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CateHomeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_cate_home, parent, false);
        return new CateHomeViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull CateHomeViewHolder holder, int position) {
        Good good = goodList.get(position);
        Glide.with(context)
                .load(good.getPgUrl())
                .fitCenter()
                .apply(new RequestOptions().transform(new RoundedCorners(32)))
                .error(R.drawable.image_error)
                .into(holder.imageView);
        holder.titleView.setText(good.getPgTitle());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.descView.setText(Html.fromHtml(good.getPgContent(),Html.FROM_HTML_MODE_COMPACT));
        }else{
            holder.descView.setText(good.getPgContent());
        }
        holder.checkDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, GoodDetailActivity.class);
                intent.putExtra("id", good.getId());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return goodList.size();
    }
}

class CateHomeViewHolder extends RecyclerView.ViewHolder{
    ImageView imageView;
    TextView titleView;
    TextView descView;
    TextView checkDetail;
    public CateHomeViewHolder(@NonNull View itemView) {
        super(itemView);
        imageView=itemView.findViewById(R.id.image_left);
        titleView=itemView.findViewById(R.id.tv_title);
        descView=itemView.findViewById(R.id.tv_desc);
        checkDetail=itemView.findViewById(R.id.tv_check);
    }
}
